package com.ls.lserp.modules.job.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ls.lserp.common.service.CrudService;
import com.ls.lserp.common.utils.ResourcesUtil;
import com.ls.lserp.dbaccess.Dao.ContractCustomOrderEntityMapper;
import com.ls.lserp.dbaccess.Dao.OrderEntityMapper;
import com.ls.lserp.dbaccess.Entity.ContractCustomOrderEntity;
import com.ls.lserp.dbaccess.Entity.OrderEntity;
import com.ls.lserp.modules.job.dao.InventoryMaterialEntityDao;
import com.ls.lserp.modules.job.dao.MaterialReserverEntityMtmMapper;
import com.ls.lserp.modules.job.entity.InventoryMaterialEntity;
import com.ls.lserp.modules.job.entity.MaterialReserverMtmEntity;
import com.ls.lserp.modules.material.dao.InventoryRequestExDao;
import com.ls.lserp.modules.material.entity.InventoryRequestEx;

/**
 * @ClassName : DataBaseSyncService
 * @Description : ERP与平台数据同步 i_inventory_material_p
 * @author : zhigang.gao
 * @date : 2017年7月6日
 */
@Service
@Transactional(readOnly = false)
public class InventoryMaterialSyncService extends CrudService<InventoryMaterialEntityDao, InventoryMaterialEntity> {

	@Autowired
	private InventoryMaterialEntityDao inventoryMaterialEntityDao;
	
	@Autowired
	private MaterialReserverEntityMtmMapper materialReserverEntityMtmMapper; // MTM.i_material_reserver_s
	
	@Autowired
	private OrderEntityMapper orderEntityMapper; // ERP.o_order_p 定制订单表
	
	@Autowired
	private ContractCustomOrderEntityMapper contractCustomOrderEntityMapper; // ERP.c_contract_custom_order_r
	
	@Autowired
	private InventoryRequestExDao inventoryRequestExDao; // ERP.i_inventory_requestex_p
	/**
	 * @Title : findAllList
	 * @Description : 取得ERP最新数据
	 * @return
	 * @throws SQLException
	 * @return : List<InventoryMaterialEntity>
	 *
	 * @author : zhigang.gao
	 * @date : 2017年7月6日
	 */
	public List<InventoryMaterialEntity> findAllList(InventoryMaterialEntity inventoryMaterialEntity)
			throws SQLException {

		return inventoryMaterialEntityDao.findAllList(inventoryMaterialEntity);
	}

	/**
	 * @Title : dataSync
	 * @Description : 同步数据
	 * @param InventoryMaterialEntity
	 * @throws SQLException
	 * @return : void
	 *
	 * @author : zhigang.gao
	 * @date : 2017年7月6日
	 */
	public void dataSync(InventoryMaterialEntity inventoryMaterialEntity) throws SQLException {

		// 清空旧数据
		//inventoryMaterialEntityDao.deleteAll(inventoryMaterialEntity);

		// 插入新数据
		//inventoryMaterialEntityDao.insertByList(inventoryMaterialEntity);
		
		for (InventoryMaterialEntity entity : inventoryMaterialEntity.getInventoryMaterialEntityList()) {
			inventoryMaterialEntityDao.insertByEntity(entity);
		}

	}

	/**
	 * @Title : selectByMaterialID
	 * @Description : 物料ID查物料库存信息
	 * @param materialId
	 * @return
	 * @return : InventoryMaterialEntity
	 *
	 * @author : zhigang.gao
	 * @date : 2017年7月10日
	 */
	public InventoryMaterialEntity selectByMaterialID(InventoryMaterialEntity inventoryMaterialEntity) {

		return inventoryMaterialEntityDao.selectByMaterialID(inventoryMaterialEntity);
	}
	
	
	/**
	 * @Title : findAllMaterialReserver
	 * @Description : 获取所有面料消耗数据
	 * @return List<MaterialReserverMtmEntity>
	 * @throws SQLException 
	 *
	 * @author : X.T.H
	 * @date : 2018年12月26日
	 */
	public List<MaterialReserverMtmEntity> findAllMaterialReserver(){
		List<MaterialReserverMtmEntity> materialReserverList = materialReserverEntityMtmMapper.findAllMaterialReserver();
		if (materialReserverList != null && materialReserverList.size() > 0) {
			OrderEntity orderEntity = null;
			for (int i = 0; i < materialReserverList.size(); i++) {
				orderEntity = this.getOrderInfoByStyleId(String.valueOf(materialReserverList.get(i).getStyleId()));
				if (orderEntity != null) {
					materialReserverList.get(i).setOrderNo(orderEntity.getOrderNo());
				}
			}
		}
		return materialReserverList;
	}
	
	/**
	 * @Title : setProductionFlag
	 * @Description : 获取所有面料消耗数据
	 * @return List<MaterialReserverMtmEntity>
	 * @throws SQLException 
	 *
	 * @author : X.T.H
	 * @date : 2018年12月26日
	 */
	public List<MaterialReserverMtmEntity> setProductionFlag(List<MaterialReserverMtmEntity> materialReserverList){
		// 返回实体
		List<MaterialReserverMtmEntity> returnList = new ArrayList<MaterialReserverMtmEntity>();
		// 出库状态 全部出库
		String inventoryStatus = ResourcesUtil.getConfig("outbound_status_all");
		// 订单数据接收实体
		OrderEntity orderEntity = null;
		// 订单数据查询实体
		OrderEntity searchOrderEntity = null;
		// 合同订单关联数据接收实体
		ContractCustomOrderEntity contractOrderEntity = null; 
		// 出库数据接收实体
		InventoryRequestEx inventoryRequestEx = null;
		// 出库数据查询实体
		InventoryRequestEx searchInventoryRequestEx = null;
		for (int i = 0; i < materialReserverList.size(); i++) {
			searchOrderEntity = new OrderEntity();
			searchOrderEntity.setOrderNo(materialReserverList.get(i).getOrderNo());
			// 根据订单号查询数据
			orderEntity = this.getOrderInfoByOrderNo(searchOrderEntity);
			if (orderEntity != null) {
				// 根据订单id查询数据
				contractOrderEntity = contractCustomOrderEntityMapper.getByOrderId(orderEntity.getOrderId());
				if (contractOrderEntity != null) {
					searchInventoryRequestEx = new InventoryRequestEx();
					searchInventoryRequestEx.setContractID(contractOrderEntity.getContractId());
					searchInventoryRequestEx.setInventoryStatus(inventoryStatus);
					// 根据合同id和出库状态查询数据
					inventoryRequestEx = inventoryRequestExDao.getByContractIdAndStatus(searchInventoryRequestEx);
					if (inventoryRequestEx != null) {
						materialReserverList.get(i).setProductionFlag(1);
						returnList.add(materialReserverList.get(i));
					}
				}
			}
		}
		// 返回数据
		return returnList;
	}
	
	public OrderEntity getOrderInfoByStyleId(String styleId){
		return orderEntityMapper.getByStyleId(styleId);
	}
	
	public OrderEntity getOrderInfoByOrderNo(OrderEntity orderEntity){
		return orderEntityMapper.getOrderLikeOrderNoLimitOne(orderEntity);
	}
	
	/**
	 * @Title : updateList
	 * @Description : 更新库存
	 * @return
	 * @throws SQLException
	 *
	 * @author : X.T.H
	 * @date : 2018年12月26日
	 */
	public void updateList(List<InventoryMaterialEntity> inventoryMaterialList)
			throws SQLException {
		if (inventoryMaterialList != null && inventoryMaterialList.size() > 0) {
			for (InventoryMaterialEntity iEntity : inventoryMaterialList) {
				inventoryMaterialEntityDao.updateMaterialQuantity(iEntity);
			}
		}
	}
	
	/**
	 * @Title : updateProductionFlag
	 * @Description : 更新预耗料生产标识
	 * @return
	 * @throws SQLException
	 *
	 * @author : X.T.H
	 * @date : 2018年12月26日
	 */
	public void updateProductionFlag(List<MaterialReserverMtmEntity> materialReserverList){
		if (materialReserverList != null && materialReserverList.size() > 0) {
			for(MaterialReserverMtmEntity reserverMtmEntity : materialReserverList){
				//
				materialReserverEntityMtmMapper.updateProductionFlag(reserverMtmEntity);
			}
		}
	}

}
